package xzr4;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MouseTest {
    private JFrame jf;
    private JLabel label;
    private JTextArea jta;

    public MouseTest() {
        jf = new JFrame("鼠标事件测试！");
        label = new JLabel(" ");
        jta = new JTextArea(30,40);
        jf.setLayout(new BorderLayout());
        jf.add(label,BorderLayout.NORTH);
        jf.add(jta,BorderLayout.CENTER);
        jta.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
               String str = null;
               switch (e.getButton()){
                   case MouseEvent.BUTTON1:str = "左";break;
                   case MouseEvent.BUTTON2:str = "中";break;
                   case MouseEvent.BUTTON3:str = "右";break;
               }
               jta.append("鼠标"+str+"键被点击("+e.getX()+","+e.getY()+")\n");
            }


            @Override
            public void mouseEntered(MouseEvent e) {
                jta.append("鼠标进入！\n");
            }

            @Override
            public void mouseExited(MouseEvent e) {
                jta.append("鼠标移除！\n");
            }
        });
        jta.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                label.setText("("+e.getX()+","+e.getY()+")\n");
            }
        });
        jf.setSize(300,300);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);
    }

    public static void main(String[] args) {
        MouseTest mouseTest = new MouseTest();
    }
}
